import { differenceBy } from 'lodash-es'

/** 分页项 */
export const pageSizeOptions = [10, 30, 50, 100, 200, 500, 1000]

/** 默认分页数 */
export const defaultPageSize = pageSizeOptions[1]

/**
 * 获取分页配置
 * @param {{ total: Number, current: Number, pageSize: Number }} pagination
 * @param {Function} pageChange
 * @returns {import('antd/lib/pagination').PaginationConfig}
 */
export const getPagination = (pagination, pageChange) => {
  return {
    pageSizeOptions,
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: total => `共 ${total} 条`,
    onShowSizeChange: pageChange,
    onChange: pageChange,
    ...pagination,
  }
}

/**
 * @typedef {Object} GetRowSelectionProps
 * @property {import('antd/lib/table/interface').TableRowSelection['type']} [type] 选择类型
 * @property {Array<{}>} [selList] 选择列表
 * @property {function(Array<{}>)} [setSelList] 设置选中列表
 * @property {string} [rowKey] 对应table的rowKey，默认id
 * @property {boolean} [keepOtherPageData] 是否保留其他页数据，默认true
 */

/**
 * 获取选择配置
 * @param {GetRowSelectionProps} param0
 * @returns {import('antd/lib/table/interface').TableRowSelection}
 */
export const getRowSelection = ({
  type = 'checkbox',
  selList = [],
  setSelList = _ => _,
  rowKey = 'id',
  keepOtherPageData = true,
}) => {
  return {
    type,
    fixed: 'left',
    selectedRowKeys: selList.map(({ [rowKey]: id }) => id),
    ...(!keepOtherPageData || type === 'radio'
      ? {
          onChange: (selectedRowKeys, selectedRows) => {
            setSelList(selectedRows)
          },
        }
      : {
          onSelectAll(selected, _, changeRows) {
            const cloneSelRow = [...selList]
            if (selected) {
              setSelList([...cloneSelRow, ...changeRows])
            } else {
              const newArr = differenceBy(cloneSelRow, changeRows, rowKey)
              setSelList(newArr)
            }
          },
          onSelect(record, selected) {
            const cloneSelRow = [...selList]
            if (selected) {
              setSelList([...cloneSelRow, record])
            } else {
              const newArr = differenceBy(cloneSelRow, [record], rowKey)
              setSelList(newArr)
            }
          },
        }),
  }
}

/** @type {import('antd/lib/config-provider').ThemeConfig} */
export const theme = {
  components: {
    Table: {
      cellPaddingBlock: 6,
      cellPaddingInline: 10,
    },
    Tooltip: {
      colorBgSpotlight: '#fff',
      colorTextLightSolid: '#000000e0',
    },
    Form: {
      itemMarginBottom: 10,
    },
    Divider: {
      verticalMarginInline: 6,
    },
    Tabs: {
      cardBg: '#efefef',
    },
  },
}

/**
 * 序号
 * @param {*} config
 * @returns
 */
export const renderIndexColumn = (config = {}) => ({
  dataIndex: 'index',
  title: '序号',
  className: 'text-center!',
  width: 50,
  render: (text, row, index) => {
    const { pagination } = config
    if (row.showSeq === false) {
      return
    }
    let seq = index + 1
    if (pagination && pagination.current && pagination.pageSize) {
      const previous = Math.max(0, pagination.current - 1) * pagination.pageSize
      seq += previous
    }
    return seq
  },
})

/**
 * @typedef {import('antd/es/modal/Modal').default & import('antd/es/modal/confirm').ModalStaticFunctions} Modal
 */

/** @type {Modal} */
export let modal

/**
 * 设置antd的Modal的实例
 * @param {Modal} _modal
 */
export const setModal = _modal => {
  modal = _modal
}

/**
 * @typedef {import('antd/es/message/interface').MessageInstance} Message
 */

/** @type {Message} */
export let message

/**
 * 设置antd的Message的实例
 * @param {Message} _message
 */
export const setMessage = _message => {
  message = _message
}
